home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / PowerPlant / AGA Classes 1.2 / CheckBox / LAGACheckBox.cp < prev    next >
Text File  |  1996-06-30  |  11KB  |  359 lines

  1. // ===========================================================================
  2. //    LAGACheckBox.cp
  3. // ===========================================================================
  4. //    “Apple Grayscale Appearance” compliant check box
  5. //    Copyright © 1996 Chrisoft (Christophe ANDRES)  All rights reserved.
  6. //
  7. //    You may use this source code in any application (commercial, shareware, freeware,
  8. //    postcardware, etc), but not remove this notice (no need to acknowledge the use of
  9. //    this class in the about box)
  10. //    You may not sell this source code in any form. This source code may be placed on 
  11. //    publicly accessable archive sites and source code disks. It may not be placed on 
  12. //    profit archive sites and source code disks without the permission of the author, 
  13. //    Christophe ANDRES.
  14. //    
  15. //        This source code is distributed in the hope that it will be useful,
  16. //        but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. //        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. //
  19. //    If you make any change or improvement on this class, please send the improved/changed
  20. //    version to : chrisoft@calva.net or Christophe ANDRES
  21. //                                     20, rue Prosper Mérimée
  22. //                                     67100 STRASBOURG
  23. //                                     FRANCE
  24. //
  25. // ===========================================================================
  26. //    LAGACheckBox.h            <- double-click + Command-D to see class declaration
  27. //
  28. //    LAGACheckBox is my implementation of the “Apple Grayscale Appearance for System 7.5”
  29. //        Check box
  30. //
  31. //        This class requires AGAColors.cp to be present in your project
  32. //
  33. //        Version : 1.2
  34. //
  35. //        Change History (most recent first, date in US form : mm/dd/yy):
  36. //
  37. //                        06/30/96    ca        Public release of version 1.2
  38. //                        06/27/96    ca        Changed checks for disabled state (check on triState_On instead of triState_Off)
  39. //                                                            in order that triState_Latent state is drawn as disabled
  40. //                        06/04/96    ca        made CW9 adjustments
  41. //                                                        Increased version to 1.2
  42. //                        05/23/96    ca        Updated the below changes (05/23/96    M™H) to allow the usage of either the standard checkbox template
  43. //                                                            or the custom LAGACheckBox template. The standard checkbox template is easier for layout
  44. //                                                            because you can see the text, but you cannot define directly a mixed value check box
  45. //                                                        Defined class_ID1 and CreateAGACheckBoxStream1 to handle the two template possibilities
  46. //                                                        Added a CPPb to "LAGACheckBox CPPb.rsrc" called LAGACheckBox1 to handle the standard checkbox
  47. //                                                            template, without having to tinker the class ID
  48. //                                                        Added a static RegisterClass method to simplify the class registration
  49. //                        05/23/96    M™H        Changed the stream constructor to use the standard Check box template in Contructor
  50. //                                                            which enables to see the text put in a Check box while defining the view
  51. //                                                        Changed drawing of monochrome checkBox to look like the standard one
  52. //                        05/15/96    ca        Increased version to 1.1
  53. //                                                        Added copy constructor
  54. //                                                        Added "on the fly" constructor
  55. //                                                        Replaced UEnvironment::HasFeature(env_SupportsColor) with PaneInColor
  56. //                                                        Added GetDescriptor and SetDescriptor
  57. //                                                        Added change history
  58. //                        04/22/96    ca        class made available by Christophe ANDRES <chrisoft@calva.net>
  59. //                                                        (version 1.0)
  60. //
  61. //        To Do:
  62. //
  63.  
  64. #include "LAGACheckBox.h"
  65.  
  66. #include "AGAColors.h"
  67.  
  68. #include <UTextTraits.h>
  69. #include <UEnvironment.h>
  70. #include <UDrawingState.h>
  71. #include <LStream.h>
  72. #include <PP_Messages.h>
  73. #include <UDrawingUtils.h>
  74.  
  75. #define    kAGACheckBoxWidth                        12
  76. #define    kAGACheckBoxHeigth                    12
  77.  
  78. //-------LAGACheckBox class---------------------------------------------------------------------------------------
  79.  
  80. //    begin <05/23/96 ca>
  81. void LAGACheckBox::RegisterClass ()
  82.  
  83. {
  84.     //    Constructing the class with the LAGACheckBox CPPb
  85.     URegistrar::RegisterClass(LAGACheckBox::class_ID, (ClassCreatorFunc)LAGACheckBox::CreateAGACheckBoxStream);
  86.     //    Constructing the class with the standard checkbox template or the LAGACheckBox1 CPPb
  87.     URegistrar::RegisterClass(LAGACheckBox::class_ID1, (ClassCreatorFunc)LAGACheckBox::CreateAGACheckBoxStream1);
  88. }
  89.  
  90. LAGACheckBox* LAGACheckBox::CreateAGACheckBoxStream (LStream *inStream)
  91.  
  92. {
  93.     //    Constructing from a 'AGA1' template
  94.     return(new LAGACheckBox(inStream));
  95. }
  96.  
  97. LAGACheckBox* LAGACheckBox::CreateAGACheckBoxStream1 (LStream *inStream)
  98.  
  99. {
  100.     //    Constructing from a 'AgA1' template
  101.     return(new LAGACheckBox(inStream, true));
  102. }
  103. //    end <05/23/96 ca>
  104.  
  105. //-------Constructors-------------------------------------------------------------------------------------------------
  106.  
  107. LAGACheckBox::LAGACheckBox ()
  108.  
  109. {
  110.     mTitle = "\p";
  111.     SetMaxValue(2);        //    2 is for mixed value Checkboxes
  112. }
  113.  
  114. LAGACheckBox::LAGACheckBox (LStream *inStream, Boolean inCreateWithStdCheckBox) : LControl(inStream)
  115.  
  116. {
  117.     if (inCreateWithStdCheckBox)                                                                                                                                                        //    <05/23/96 ca>
  118.         {
  119.             // begin <05/23/96 M™H> mod
  120.             // Change so we can use the standard checkbox template; makes layout easier
  121.             Int16        controlKind;
  122.             Int32        macRefCon;
  123.             
  124.             inStream->ReadData(&controlKind, sizeof(Int16));                //    Ignored
  125.             inStream->ReadData(&mTextTraitsID, sizeof(ResIDT));
  126.             inStream->ReadPString(mTitle);
  127.             inStream->ReadData(&macRefCon, sizeof(Int32));                    //    Ignored
  128.             // end <05/23/96 M™H>
  129.         }
  130.     else
  131.         {
  132.             inStream->ReadData(&mTextTraitsID, sizeof(ResIDT));
  133.             inStream->ReadPString(mTitle);
  134.         }
  135.  
  136.     if (mMaxValue < 2)
  137.         SetMaxValue(2);
  138. }
  139.  
  140. //    begin    <05/15/96    ca>
  141. LAGACheckBox::LAGACheckBox (const LAGACheckBox &inOriginal) : LControl(inOriginal)
  142.  
  143. {
  144.     mTextTraitsID = inOriginal.mTextTraitsID;
  145.     mTitle = inOriginal.mTitle;
  146.     if (mMaxValue < 2)
  147.         SetMaxValue(2);
  148. }
  149.  
  150. LAGACheckBox::LAGACheckBox (const SPaneInfo &inPaneInfo, MessageT inValueMessage, Int32 inValue,
  151.                                                         ResIDT inTextTraitsID, Str255 inTitle) : LControl(inPaneInfo, inValueMessage, inValue, 0, 2)
  152. {
  153.     mTextTraitsID = inTextTraitsID;
  154.     mTitle = inTitle;
  155. }
  156. //    end    <05/15/96    ca>
  157.  
  158. //-------Drawers----------------------------------------------------------------------------------------------------
  159.  
  160. void LAGACheckBox::DrawSelf ()
  161.  
  162. {
  163.     DrawGraphic();
  164.     DrawText();
  165. }
  166.  
  167. void LAGACheckBox::DrawGraphic (Boolean inPushed)
  168.  
  169. {
  170.     StColorPenState theState;
  171.     Rect frame;
  172.     Boolean hasColor = ::PaneInColor(this);
  173.     Boolean disabled = (mEnabled != triState_On);                                                                                                                //    <06/27/96    ca>
  174.     
  175.     theState.Normalize();
  176.     
  177.     CalcLocalFrameRect(frame);
  178.     frame.right = frame.left + kAGACheckBoxWidth;
  179.     frame.bottom = frame.top + kAGACheckBoxHeigth;
  180.     
  181.     //    Draw the frame of the checkbox
  182.         if (disabled)
  183.             if (hasColor)
  184.                 ::RGBForeColor(&gAGAColorArray[7]);
  185.             else
  186.                 ::PenPat(&qd.gray);
  187.         
  188.     ::FrameRect(&frame);
  189.     
  190.     if (!hasColor)
  191.         PenNormal();
  192.         
  193.     ::InsetRect(&frame, 1, 1);
  194.     if (hasColor)
  195.         ::RGBBackColor(inPushed ? &gAGAColorArray[8] : &gAGAColorArray[2]);
  196.     ::EraseRect(&frame);
  197.     
  198.     if (hasColor)
  199.         {
  200.             if (inPushed)
  201.                 ::RGBForeColor(&gAGAColorArray[10]);
  202.             else
  203.                 ForeColor(whiteColor);
  204.             ::MoveTo(frame.left, frame.bottom - 2);
  205.             ::LineTo(frame.left, frame.top);
  206.             ::LineTo(frame.right - 2, frame.top);
  207.     
  208.             if (inPushed)
  209.                 ::RGBForeColor(&gAGAColorArray[6]);
  210.             else
  211.                 ::RGBForeColor(mEnabled == triState_On ? &gAGAColorArray[7] : &gAGAColorArray[5]);                    //    <06/27/96    ca>
  212.             ::MoveTo(frame.left + 1, frame.bottom - 1);
  213.             ::LineTo(frame.right - 1, frame.bottom - 1);
  214.             ::LineTo(frame.right - 1, frame.top + 1);
  215.         }
  216.     
  217.     if (mValue)
  218.         {
  219.             if (mValue == 1)
  220.                 {
  221.                     //    Checked state
  222.                     if (hasColor)
  223.                         {
  224.                             ::RGBForeColor(inPushed ? &gAGAColorArray[10] : &gAGAColorArray[5]);
  225.                             ::MoveTo(frame.left + 3, frame.bottom - 2);
  226.                             ::LineTo(frame.right - 2, frame.top + 3);
  227.                             ::MoveTo(frame.right - 2, frame.bottom - 2);
  228.                             ::LineTo(frame.right - 2, frame.bottom - 2);
  229.                     
  230.                             if (mEnabled == triState_On)                                                                                                                    //    <06/27/96    ca>
  231.                                 ::RGBForeColor(inPushed ? &gAGAColorArray[11] : &gAGAColorArray[8]);
  232.                             ::MoveTo(frame.left + 3, frame.bottom - 3);
  233.                             ::LineTo(frame.right - 2, frame.top + 2);
  234.                             ::MoveTo(frame.right - 2, frame.bottom - 3);
  235.                             ::LineTo(frame.right - 2, frame.bottom - 3);
  236.                             
  237.                             if (mEnabled == triState_On)                                                                                                                    //    <06/27/96    ca>
  238.                                 ::ForeColor(blackColor);
  239.                             else
  240.                                 ::RGBForeColor(&gAGAColorArray[7]);
  241.                             // begin <05/23/96 M™H> mod
  242.                             // Make the monochrome version look more like the original
  243.                             ::MoveTo(frame.left + 2, frame.top + 1);
  244.                             ::LineTo(frame.right - 3, frame.bottom - 4);
  245.                             ::MoveTo(frame.left + 2, frame.top + 2);
  246.                             ::LineTo(frame.right - 3, frame.bottom - 3);
  247.                             ::MoveTo(frame.left + 2, frame.bottom - 4);
  248.                             ::LineTo(frame.right - 3, frame.top + 1);
  249.                             ::MoveTo(frame.left + 2, frame.bottom - 3);
  250.                             ::LineTo(frame.right - 3, frame.top + 2);
  251.                         }
  252.                     else
  253.                         {
  254.                             ::MoveTo(frame.left, frame.top);
  255.                             ::LineTo(frame.right - 1, frame.bottom - 1);
  256.                             ::MoveTo(frame.left, frame.bottom - 1);
  257.                             ::LineTo(frame.right - 1, frame.top);
  258.                         }
  259.                         // end <05/23/96 M™H> mod
  260.                 }
  261.             else
  262.                 {
  263.                     //    Mixed state
  264.                     if (hasColor)
  265.                         {
  266.                             ::RGBForeColor(inPushed ? &gAGAColorArray[10] : &gAGAColorArray[5]);
  267.                             ::MoveTo(frame.left + 3, frame.top + 6);
  268.                             ::LineTo(frame.right - 2, frame.top + 6);
  269.                             ::LineTo(frame.right - 2, frame.top + 4);
  270.                     
  271.                             if (mEnabled == triState_On)                                                                                                                    //    <06/27/96    ca>
  272.                                 ::ForeColor(blackColor);
  273.                             else
  274.                                 ::RGBForeColor(&gAGAColorArray[7]);
  275.                         }
  276.                     ::MoveTo(frame.left + 2, frame.top + 4);
  277.                     ::LineTo(frame.right - 3, frame.top + 4);
  278.                     ::MoveTo(frame.left + 2, frame.top + 5);
  279.                     ::LineTo(frame.right - 3, frame.top + 5);
  280.                 }
  281.         }
  282.         
  283.     if (inPushed && !hasColor)
  284.         InvertRect(&frame);
  285. }
  286.  
  287. void LAGACheckBox::DrawText ()
  288.  
  289. {
  290.     StColorPenState theState;
  291.     // Retrieve info about font again.
  292.  
  293.     Int16 just = UTextTraits::SetPortTextTraits(mTextTraitsID);
  294.     
  295.     Rect frame;
  296.     CalcLocalFrameRect(frame);
  297.     frame.left += kAGACheckBoxWidth + 5;
  298.     frame.top--;
  299.     frame.bottom--;
  300.     if (UEnvironment::HasFeature(env_SupportsColor))
  301.         {
  302.             RGBColor textColor;
  303.             ::GetForeColor(&textColor);
  304.     
  305.             ApplyForeAndBackColors();
  306.             if (mEnabled == triState_On)                                                                                                                                    //    <06/27/96    ca>
  307.                 ::RGBForeColor(&textColor);
  308.             else
  309.                 ::RGBForeColor(&gAGAColorArray[7]);
  310.         }
  311.     UTextDrawing::DrawWithJustification((Ptr)&mTitle[1], mTitle[0], frame, just);
  312. }
  313.  
  314. //-------Utilities--------------------------------------------------------------------------------------------------
  315.  
  316. StringPtr LAGACheckBox::GetDescriptor (Str255 outDescriptor) const
  317.  
  318. {
  319.     return LString::CopyPStr(mTitle, outDescriptor);
  320. }
  321.  
  322. void LAGACheckBox::SetDescriptor (ConstStr255Param inDescriptor)
  323.  
  324. {
  325.     mTitle = inDescriptor;
  326.     Refresh();
  327. }
  328.  
  329. void LAGACheckBox::SetValue (Int32 inValue)
  330.  
  331. {
  332.     if (inValue != mValue)
  333.         {
  334.             LControl::SetValue(inValue);
  335.             FocusDraw();                // Redraw to reflect new state
  336.             DrawGraphic();
  337.         }
  338. }
  339.  
  340. void LAGACheckBox::HotSpotAction (Int16 /*inHotSpot*/, Boolean inCurrInside, Boolean inPrevInside)
  341.  
  342. {
  343.     // Draw if cursor moved from IN to OUT
  344.     //   or from OUT to IN
  345.     if (inCurrInside != inPrevInside)
  346.         {
  347.             FocusDraw();
  348.             DrawGraphic(inCurrInside);
  349.         }
  350. }
  351.  
  352. void LAGACheckBox::HotSpotResult (Int16 /*inHotSpot*/)
  353.  
  354. {
  355.     SetValue(1 - GetValue());
  356. }
  357.  
  358.  
  359.